import {
	apiGetYuanData,
	apiGetYuanDataDeatil,
	apiDelYuanData,
	apiAddYuanData,
	apiUpdataYuanData,
	apiConnDataList,
	apiConnDataDetail,
	apiConnDataDel,
	apiConnDataAdd,
	apiConnDataUpdata,
	apiConnDataUp,
	apiGetArea,
	apiGetYwType,
	apiGetType,
	apiSelectAll,
	apiSelectAreaById,
	apiConnDataOnly,
} from "./apiFuwuServer"

export const componentGetYuanData = (ev, param) => {/*获取元数据*/
	apiGetYuanData(
		{
			"search": param.search,//字段值
			"sortTime": param.sortTime,//创建时间排序字段，只能写asc(升序)或desc（降序），不写默认asc
			"sortNumber": param.sortNumber,//引用数量排序，只能写asc(升序)或desc（降序）
			"pageNum": param.pageNum,//分页页数，从1开始
			"pageSize": param.pageSize//显示条数
		}
	).then((res) => {
		ev.total = res.data.data.total
		ev.form.pageSize = res.data.data.pageSize
		if (res.data.code === 'S100101') {
			ev.dataManage = res.data.data.list;
		}
		
	})
};

export const componentGetYuanDataDeatil = (ev, param) => {/*获取元数据详情*/
	apiGetYuanDataDeatil(param).then((res) => {
		if (res.data.code === 'S100101') {
			ev.detailData = res.data.data;
		}
	})
};

export const componentDelYuanData = (ev, param, evForm) => {/*删除元数据*/
	ev.$confirm('是否删除?', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	}).then(() => {
		apiDelYuanData(param).then((res) => {
			if (res.data.code === 'S100101') {
				ev.$message({
					type: 'success',
					message: '删除成功!'
				});
				let data = {
					"search": '',
					"sortTime": 'desc',
					"sortNumber": 'desc',
					"pageNum": 1,
					"pageSize": 6
				}
				//刷新数据
				componentGetYuanData(ev, evForm)
				ev.pageNo = 1;
				ev.form.pageNum = 1
			}
		})
	}).catch(() => {
		ev.$message({
			type: 'info',
			message: '已取消删除'
		});
	});


};



export const componentAddYuanData = (ev, param) => {/*插入元数据*/
	apiAddYuanData({
		"name": param.zdName,
		"key": param.zdval,
		"dataType": param.type,
		"type": param.shiyongType,
		"minLen": param.minWidth,
		"maxLen": param.maxWidth,
		"ruleIds": param.resource,
		"otherRule": param.desc,
	}).then((res) => {
		if (res.data.code === 'S100101') {
			ev.$message({
				type: 'success',
				message: '新增成功!'
			});
			let data = {
				"search": '',
				"sortTime": 'desc',
				"sortNumber": 'desc',
				"pageNum": 1,
				"pageSize": 6
			}
			//刷新数据
			componentGetYuanData(ev, data)
			ev.pageNo = 1;
			ev.form.pageNum = 1
		} else {
			ev.$message({
				type: 'info',
				message: res.data.msg
			});
		}
	})
}

export const componentUpdataYuanData = (ev, ids, param) => {/*修改元数据*/
	apiUpdataYuanData({
		"id": ids.id,
		"name": param.name,
		"key": param.key,
		"dataType": param.dataType,
		"type": param.type,
		"minLen": param.minLen,
		"maxLen": param.maxLen,
		"ruleIds": param.ruleIds,
		"otherRule": param.otherRule,
	}).then((res) => {
		if (res.data.code === 'S100101') {
			ev.$message({
				type: 'success',
				message: '修改成功!'
			});
			let data = {
				"search": '',
				"sortTime": 'desc',
				"sortNumber": 'desc',
				"pageNum": 1,
				"pageSize": 15
			}
			//刷新数据
			componentGetYuanData(ev, data)
			ev.pageNo = 1;
			ev.form.pageNum = 1
		} else {
			ev.$message({
				type: 'info',
				message: res.data.msg
			});
		}
	})
}

export const componentConnDataList = (ev, param) => {/*数据源列表*/
	let type = ["", "csv文件", "mysql5.7", "oracle11", "xml文件", "json文件"];
	ev.loading = true;
	apiConnDataList({
		"name": param.name,
		"pageNum": param.pageNum,
		"pageSize": param.pageSize,
	}).then((res) => {
		if (res.data.code === 'S100101') {
			res.data.data.list.map((item)=>{/*因为有时候后台会返回null*/
				item.typeName= item.typeName==null?'数据库类型':item.typeName;
			})
			ev.tableData = res.data.data.list;
			ev.pageTotal = res.data.data.total;  //查询数据总数
		}
		ev.loading = false;
	})
}


export const componentConnDataDetail = (ev, param) => {/*数据源详情*/
	apiConnDataDetail(param).then((res) => {
		if (res.data.code === 'S100101') {
			console.log(res.data.data);
			apiSelectAreaById( res.data.data.areaId).then((res2)=>{
				console.log(res2);
				res.data.data.areaIdAndName = res2.data.data.name  
				ev.formData = res.data.data;
			})
			
		}

	})
}

export const componentConnDataUp = (ev, param) => {/*数据源更新*/
	apiConnDataUp(param).then((res) => {
		if(res.data.code === 'S100101'){
			if(res.data.data === true){
				ev.$message("数据源更新成功")
			}else{
				ev.$message("数据源更新失败")
			}
		}else{
			console.log('出错了');
		}

	})
}

export const componentConnDataDel = (ev, param) => {/*删除数据源*/
	ev.$confirm('是否删除?', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	}).then(() => {
		apiConnDataDel(param).then((res) => {
			if (res.data.code === 'S100101') {
				ev.$message({
					type: 'success',
					message: '删除成功!'
				});
				//刷新数据
				componentConnDataList(ev, param)
				ev.pageNo=1;
			} else {
				ev.$message({
					type: 'info',
					message: '删除失败'
				});
			}
		})
	}).catch(() => {
		ev.$message({
			type: 'info',
			message: res.data.msg
		});
	});
}


export const componentConnDataAdd = (ev, param) => {/*增加数据源*/
	apiConnDataAdd(param).then((res) => {
		if (res.data.code === 'S100101') {
			ev.$message({
				type: 'success',
				message: '添加成功!'
			})
			//刷新数据
			let data = {
				"name": '',
				"pageNum": param.pageNum,
				"pageSize": param.pageSize,
			}
			componentConnDataList(ev, data)
			ev.closeForm();
			ev.pageNo=1;
			
		} else if(res.data.code === 'SM200511'){
			ev.$message({
				type: 'info',
				message: '数据源名称已存在'
			});
		} else {
			ev.$message({
				type: 'info',
				message: '添加失败'
			});
		}

	})
}

export const componentConnDataUpdata = (ev, param, id) => {/*修改数据源*/
	param.id = id;
	apiConnDataUpdata(param).then((res) => {
		if (res.data.code === 'S100101') {
			ev.$message({
				type: 'success',
				message: '修改成功!'
			});
			//刷新数据
			let data = {
				"name": '',
				"pageNum": param.pageNum,
				"pageSize": param.pageSize,
			}
			componentConnDataList(ev, data)
				ev.closeForm();
				ev.pageNo=1;
		} else {
			ev.$message({
				type: 'info',
				message: '修改失败'
			});
		}
	})
}

export const componentGetArea = (ev, param) => {/*获取所在区域*/
	apiGetArea(param).then((res) => {
		if (res.data.code === 'S100101') {
			ev.formLabel[2].options = res.data.data.list;
		}
	})
}


export const componentGetYwType = (ev, param) => {/*获取业务类型，网段区域，信息类型*/
	apiGetYwType(param).then((res) => {
		if (res.data.code === 'S100101') {
//			if (res.data.data.list[0].type === "segment") {
//				ev.formLabel[1].options = res.data.data.list[0].childList;
//			}
			return ;
		}
	})
}

export const componentSelectAll=(ev)=>{/*获取网段，新的接口*/
	apiSelectAll().then((res)=>{
		ev.formLabel[1].options = res.data.data;
	})
}

export const componentGetType = (ev, param) => {/*获取业务类型，网段区域，信息类型*/
	apiGetYwType(param).then((res) => {
		if (res.data.code === 'S100101') {
			if (res.data.data.list[0].type === "db_type") {
				ev.formLabel[4].options = res.data.data.list[0].childList;
			}
		}
	})
};

//检查数据源是否唯一
export const componentConnDataOnly = (ev,param,id) => {
	if (id == 0){
		id = '';
	}
	apiConnDataOnly(param,id).then((res) => {
		console.log(res)
		if (res.data.code === 'S100101') {
			if(res.data.data){
				ev.panDuan = 'have'
			}
		}
	})
};
